/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.crystalrange.documents.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.crystalrange.model.User;

/**
 * The Class DocumentGroup.
 *
 * @author Krzysiek
 */
public class DocumentGroup {
    private Long id;
    
    /** The name. */
    private String name;
    private String description;
    private Date createdDate;
    private User createdBy;
    private List<DocumentGroup> parentDocumentGroups;
    private List<DocumentGroup> childDocumentGroups;
    private List<Document> documents;
    
    public DocumentGroup() {
        this.parentDocumentGroups = new ArrayList<DocumentGroup>();
        this.childDocumentGroups = new ArrayList<DocumentGroup>();
        this.documents = new ArrayList<Document>();
    }
    
    /**
     * Adds the parent document group.
     *
     * @param parentDocumentGroup the parent document group
     */
    public void addParentDocumentGroup(DocumentGroup parentDocumentGroup) {
    	
    	if (!parentDocumentGroup.getChildDocumentGroups().contains(this)) {
    		parentDocumentGroup.getChildDocumentGroups().add(this);
    	}
    	
    	if (!this.getParentDocumentGroups().contains(parentDocumentGroup)) {
    		this.getParentDocumentGroups().add(parentDocumentGroup);
    	}
    }
    
    /**
     * Removes the parent document group.
     *
     * @param parentDocumentGroup the parent document group
     */
    public void removeParentDocumentGroup(DocumentGroup parentDocumentGroup) {
    	
    	if (parentDocumentGroup.getChildDocumentGroups().contains(this)) {
    		parentDocumentGroup.getChildDocumentGroups().remove(this);
    	}
    	
    	if (this.getParentDocumentGroups().contains(parentDocumentGroup)) {
    		this.getParentDocumentGroups().remove(parentDocumentGroup);
    	}
    }
    
    /**
     * Adds the child document group.
     *
     * @param childDocumentGroup the child document group
     */
    public void addChildDocumentGroup(DocumentGroup childDocumentGroup) {
    	
    	if (!childDocumentGroup.getParentDocumentGroups().contains(this)) {
    		childDocumentGroup.getParentDocumentGroups().add(this);
    	}
    	
    	if (!this.getChildDocumentGroups().contains(childDocumentGroup)) {
    		this.getChildDocumentGroups().add(childDocumentGroup);
    	}
    }
    
    /**
     * Removes the child document group.
     *
     * @param childDocumentGroup the child document group
     */
    public void removeChildDocumentGroup(DocumentGroup childDocumentGroup) {
    	
    	if (childDocumentGroup.getParentDocumentGroups().contains(this)) {
    		childDocumentGroup.getParentDocumentGroups().remove(this);
    	}
    	
    	if (this.getChildDocumentGroups().contains(childDocumentGroup)) {
    		this.getChildDocumentGroups().remove(childDocumentGroup);
    	}
    }
    
    /**
     * Adds the document.
     *
     * @param document the document
     */
    public void addDocument(Document document) {
    	
    	if (!document.getDocumentGroups().contains(this)) {
    		document.getDocumentGroups().add(this);
    	}
    	
    	if (!this.getDocuments().contains(document)) {
    		this.getDocuments().add(document);
    	}
    }
    
    /**
     * Removes the document.
     *
     * @param document the document
     */
    public void removeDocument(Document document) {
    	
    	if (document.getDocumentGroups().contains(this)) {
    		document.getDocumentGroups().remove(this);
    	}
    	
    	if (this.getDocuments().contains(document)) {
    		this.getDocuments().remove(document);
    	}
    }
            
    public User getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(User createdBy) {
        this.createdBy = createdBy;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<DocumentGroup> getChildDocumentGroups() {
        return childDocumentGroups;
    }

    public void setChildDocumentGroups(List<DocumentGroup> childDocumentGroups) {
        this.childDocumentGroups = childDocumentGroups;
    }

    public List<DocumentGroup> getParentDocumentGroups() {
        return parentDocumentGroups;
    }

    public void setParentDocumentGroups(List<DocumentGroup> parentDocumentGroups) {
        this.parentDocumentGroups = parentDocumentGroups;
    }

    public List<Document> getDocuments() {
        return documents;
    }

    public void setDocuments(List<Document> documents) {
        this.documents = documents;
    }
}
